package com.rural.bus;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.rural.bus.model.Order;
import com.rural.bus.utils.DateUtil;
import com.rural.bus.view.CommonAlertDialog;
import com.rural.bus.view.CommonAlertDialog.OnDialogDismissListener;

public class OrderDetailActivity extends BaseActivity  {

	private static final String TAG = "OrderDetailActivity";
	/***
	 * 响应的message附加消息
	 */
	public static String extra;

//	private Button btn_order_operation;
//	private CircleImageView iv_passenger_head;
	private TextView tv_driver_name, tv_order_type, tv_order_distance,
			tv_start_time, tv_start_location, tv_end_location,
			tv_passenger_message,tv_price;
	private ImageView iv_call;
	private LinearLayout ll_passenger_message;
	
//	CommonAlertDialog dialog1, dialog2, dialog3;

	private Order order;
//	private int order_id;
//	private String extra;
	public static boolean isAutoToPay;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_order_detail);
		
		initView();
		setListeners();
		initData();

	}

	@Override
	protected void initView() {
		mBtMiddle.setText("订单详情");
		mBtMiddle.setVisibility(View.VISIBLE);
		mBtMiddle.setTextColor(getResources().getColor(R.color.white));

		mImgLeft.setVisibility(View.VISIBLE);
		mImgLeft.setBackgroundResource(R.drawable.back_bg_selector);

//		btn_order_operation = (Button) findViewById(R.id.btn_order_operation);
//		iv_passenger_head = (CircleImageView) findViewById(R.id.iv_passenger_head);
		tv_driver_name = (TextView) findViewById(R.id.tv_driver_name);
		tv_order_type = (TextView) findViewById(R.id.tv_order_type);
		tv_order_distance = (TextView) findViewById(R.id.tv_order_distance);
		tv_start_time = (TextView) findViewById(R.id.tv_start_time);
		tv_start_location = (TextView) findViewById(R.id.tv_start_location);
		tv_end_location = (TextView) findViewById(R.id.tv_end_location);
		tv_passenger_message = (TextView) findViewById(R.id.tv_passenger_message);
		tv_price = (TextView) findViewById(R.id.tv_price);
		iv_call=(ImageView) findViewById(R.id.iv_call);
		
		ll_passenger_message = (LinearLayout) findViewById(R.id.ll_passenger_message);
	}

	@Override
	public void initData() {
		update();
	}

//
	@Override
	public void setListeners() {

		mImgLeft.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				OrderDetailActivity.this.finish();
			}
		});
		
		iv_call.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+order.driverPhone));  
                startActivity(intent); 
			}
		});
		
	}
	
	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		update();
	}
	
	private void update(){
		order=(Order) getIntent().getSerializableExtra("order");
		
		if(isAutoToPay){
			Intent intent1 = new Intent(this, PayActivity.class);
			intent1.putExtra("order", order);
			startActivity(intent1);
			
		}
				
		updateView();
	}
	
	/***
	 * 更新view
	 */
	private void updateView(){
		if(order==null){
			return;
		}
		tv_driver_name.setText(order.driverName+"("+order.getCarType()+")");
		tv_start_location.setText(order.startLocation);
		tv_end_location.setText(order.endLocation);
		if(!TextUtils.isEmpty(order.remarks)){
			tv_passenger_message.setText(order.remarks);
		}
		tv_start_time.setText(DateUtil.getStr(order.takeTime,"yyyy年MM月dd日 HH:mm"));
		tv_order_type.setText(order.getOrderType());
		if(!TextUtils.isEmpty(extra) && !isAutoToPay){
			CommonAlertDialog dialog = new CommonAlertDialog(this, "知道了", null, extra, new OnDialogDismissListener() {

				@Override
				public void onConfirmClicked() {
					
				}

				@Override
				public void onCancelClicked() {

				}
			});
			dialog.show();
		}
		if(order.orderStatus==Order.ORDERSTATUS_REACHED||
				order.orderStatus==Order.ORDERSTATUS_COMPLETEED){
			tv_price.setText(String.format(tv_price.getText().toString(), order.finalPrice));
		}else{
			tv_price.setVisibility(View.GONE);
		}
		tv_order_distance.setText("总计"+order.distance+"公里");
		
		if(!TextUtils.isEmpty(order.remarks)){
			ll_passenger_message.setVisibility(View.VISIBLE);
			tv_passenger_message.setText(""+order.remarks);
		}else{
			ll_passenger_message.setVisibility(View.GONE);
		}
		
//		if(order.orderStatus==Order.ORDERSTATUS_REACHED){
//			btn_order_operation.setVisibility(View.VISIBLE);
//		}
//		tv_carnumber.setText(order.)
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		extra=null;
	}

}
